@if {%_ECHOON%}=={} echo off

rem #
rem # Simple batch file to set the TV2 root
rem #

@if "%1"=="/?" goto USAGE
@if "%1"=="-?" goto USAGE

@set _NT_SYMBOL_PATH=c:\symbols;C:\Windows\Symbols;
@set _CONFIG=%1
@set _TYPE=%2
@set _BUILDTYPE=%3
@set BMCONFIG=%4
@set _DRIVE=%~d0

@if "%5"=="1" set SIGNING=1

rem Set UDS_ROOT to the location of the build tree root. %~dp0 returns the
rem drive and path portion of the name of this script (%0). To get rid of
rem the trailing slash, CD to the directory, then set UDS_ROOT to the output
rem of the "cd" command.
pushd %~dp0.
@for /f %%d in ('cd') do set UDS_ROOT=%%d
popd

rem if the root ends up being something like "c:\", get rid of that pesky \ at the end
@if "%UDS_ROOT%"=="%~d0\" set UDS_ROOT=%~d0

@if '%_BUILDTYPE%'=='server' set SERVERBUILD=1
@if '%_BUILDTYPE%'=='client' set CLIENTBUILD=1
@if '%_BUILDTYPE%'=='tools' set SERVERBUILD=1

@if '%BMCONFIG%'=='PerseusHarness_debug' set StrongNamed=false
@if '%_CONFIG%'=='Installer_x64' set _PLATFORM=x64

@set _BUILD_DIR=%UDS_ROOT%\Builds
@set MSBuildRulesDir=%UDS_ROOT%\tools\Build\Rules
@set _LOCALDEVBUILD=yes

@set _FWDIR=%SystemRoot%\Microsoft.NET\Framework\v3.5
@if not exist "%_FWDIR%" set _FWDIR=
@if '%_FWDIR%'=='' goto NOFRAMEWORK

@if not exist "%VS90COMNTOOLS%vsvars32.bat" (
	@REM Add MSBuild.exe to the path
    @set "PATH=%_FWDIR%;%PATH%"
	
	@REM Add some .net tools to the path, e.g. xsd.exe
    @call :SetWindowsSdkDir
) else (
    @call "%VS90COMNTOOLS%vsvars32.bat"
)
REM if exist "%VS90COMNTOOLS%vsvars32.bat" call "%VS90COMNTOOLS%vsvars32.bat"

title %_CONFIG% (%_TYPE%) - %UDS_ROOT%

@echo -------------------------------
@echo.
@echo Solutions UDS Build Window
@echo   * Use 'msbuild' or 'msbuild [dirs.proj or yourproj.csproj]' to build.
@echo   * Use 'msbuild /t:help' to see available build targets.
@echo   * Build output goes to '%_BUILD_DIR%\%_CONFIG%\%_TYPE%\bin'.
@echo   * Build log goes to '%UDS_ROOT%\msbuild_%_CONFIG%_%_TYPE%.log'.

@goto END

:NOFRAMEWORK
@echo.
@echo Could not find the right version of the .NET framework.
@echo.
@pause
@goto END


@REM -----------------------------------------------------------------------
:SetWindowsSdkDir
@set WindowsSdkDir=
@call :GetWindowsSdkDirHelper HKLM > nul 2>&1
@if errorlevel 1 call :GetWindowsSdkDirHelper HKCU > nul 2>&1
@set "PATH=%WindowsSdkDir%bin;%PATH%"
@exit /B 0

:GetWindowsSdkDirHelper
@echo #### Calling GetWindowsSdkDirHelper
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows" /v "CurrentInstallFolder"') DO (
    @if "%%i"=="CurrentInstallFolder" (
        @SET "WindowsSdkDir=%%k"
    )
)
@if "%WindowsSdkDir%"=="" exit /B 1
@exit /B 0


@REM -----------------------------------------------------------------------
:END
